package com.defysoft.waukids;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Sqlite_Connect {

	Cursor cursor;
	Sqlite_OpenHelper so;
	public SQLiteDatabase db;

	public Sqlite_Connect(Context context) {

		so = new Sqlite_OpenHelper(context, "waukids.db", null, 1);
		this.db = so.getWritableDatabase();
	}

	public void close_Connect() {
		so.close();
		db.close();
	}

	/**
	 * 원아수첩 조회수채크 여부검사 매개인자로 글의 pk값 과 지금 로그인된 학생의 코드값을 넘기면 db에 조회된 값이 있으면 true
	 * 없으면 false반환
	 * */
	public boolean check_Notice(int pkcode, int stucode) {

		cursor = db.rawQuery("select * from Notice_Check where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode, null);

		if (cursor.getCount() == 0) {
			return false;
		} else {
			return true;
		}
	}

	public boolean stamp_Notice(int pkcode, int stucode) {

		cursor = db.rawQuery("select * from Notice_Stamp where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode, null);

		if (cursor.getCount() == 0) {
			return false;
		} else {
			return true;
		}
	}

	public boolean check_Diary(int pkcode, int stucode) {

		cursor = db.rawQuery("select * from Diary_Check where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode, null);

		if (cursor.getCount() == 0) {
			return false;
		} else {
			return true;
		}
	}

	public boolean check_Photo(int pkcode, int stucode) {
		cursor = db.rawQuery("select * from Photo_Check where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode, null);

		if (cursor.getCount() == 0) {
			return false;
		} else {
			return true;
		}
	}

	public boolean stamp_Photo(int pkcode, int stucode) {
		cursor = db.rawQuery("select * from Photo_Stamp where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode, null);

		if (cursor.getCount() == 0) {
			return false;
		} else {
			return true;
		}
	}
//////////////////////////////////////////////////////
	public boolean check_New_Notice(int pkcode, int stucode) {

		cursor = db.rawQuery(
				"select * from New_Notice_Check where  Notice_Code ="+pkcode, null);
		if (cursor.getCount() == 0) {
			return false;
		} else {

			return true;
		}
	}

	public boolean check_New_Diary(int pkcode, int stucode) {

		cursor = db.rawQuery(
				"select * from New_Notice_Check where  Diary_Code ="+pkcode, null);
		if (cursor.getCount() == 0) {
			return false;
		} else {
			return true;
		}
	}

	public boolean check_New_Photo(int pkcode) {

		cursor = db.rawQuery(
				"select * from New_Notice_Check where  Photo_Code ="+pkcode, null);
		if (cursor.getCount() == 0) {
			return false;
		} else {
			return true;
		}
	}

	public boolean check_New_Massage(int pkcode) {

		cursor = db.rawQuery("select * from New_Notice_Check where   Mas_Code ="+pkcode,
				null);
		if (cursor.getCount() == 0) {
			return false;
		} else {
			return true;
		}
	}

	public boolean check_New_Caleander(int pkcode) {

		cursor = db.rawQuery("select * from New_Notice_Check where  Cal_Code ="+pkcode,
				null);
		if (cursor.getCount() == 0) {
			return false;
		} else {
			return true;
		}
	}
	
	public boolean check_New_Notice() {

		cursor = db.rawQuery(
				"select * from New_Notice_Check where Notice_Code>0", null);
		if (cursor.getCount() == 0) {
			return false;
		} else {
			return true;
		}
	}

	public boolean check_New_Diary( ) {

		cursor = db.rawQuery(
				"select * from New_Notice_Check where  Diary_Code>0", null);
		if (cursor.getCount() == 0) {
			return false;
		} else {
			return true;
		}
	}

	public boolean check_New_Photo() {

		cursor = db.rawQuery(
				"select * from New_Notice_Check where   Photo_Code>0", null);
		if (cursor.getCount() == 0) {
			return false;
		} else {
			return true;
		}
	}

	public boolean check_New_Massage( ) {

		cursor = db.rawQuery("select * from New_Notice_Check where   Mas_Code>0",
				null);
		if (cursor.getCount() == 0) {
			return false;
		} else {
			return true;
		}
	}

	public boolean check_New_Caleander( ) {

		cursor = db.rawQuery("select * from New_Notice_Check where  Cal_Code>0",
				null);
		if (cursor.getCount() == 0) {
			return false;
		} else {
			return true;
		}
	}
////////////////////////////////////////////////////////////////////////////////
	public boolean insert_Notice(int pkcode, int stucode) {

		cursor = db.rawQuery("select * from Notice_Check where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode, null);
		if (cursor.getCount() == 0) {
			db.execSQL("insert into Notice_Check values(" + pkcode + ","
					+ stucode + ")");
		}
		return true;
	}

	public boolean insert_NoticeStamp(int pkcode, int stucode) {

		cursor = db.rawQuery("select * from Notice_Stamp where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode, null);
		if (cursor.getCount() == 0) {
			db.execSQL("insert into Notice_Stamp values(" + pkcode + ","
					+ stucode + ")");
		}
		return true;
	}

	public boolean insert_Diary(int pkcode, int stucode) {

		cursor = db.rawQuery("select * from Diary_Check where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode, null);
		if (cursor.getCount() == 0) {
			db.execSQL("insert into Diary_Check values(" + pkcode + ","
					+ stucode + ")");
		}
		return true;
	}

	public boolean insert_Photo(int pkcode, int stucode) {

		cursor = db.rawQuery("select * from Photo_Check where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode, null);
		if (cursor.getCount() == 0) {
			db.execSQL("insert into Photo_Check values(" + pkcode + ","
					+ stucode + ")");
		}
		return true;
	}

	public boolean insert_PhotoStamp(int pkcode, int stucode) {

		cursor = db.rawQuery("select * from Photo_Stamp where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode, null);
		if (cursor.getCount() == 0) {
			db.execSQL("insert into Photo_Stamp values(" + pkcode + ","
					+ stucode + ")");
		}
		return true;
	}

	public boolean insert_New_Notice(int pkcode, int stucode) {
		
		cursor = db.rawQuery(
				"select * from New_Notice_Check where Notice_Code=" + pkcode
						+ " AND " + "Stu_Code=" + stucode, null);
		if (cursor.getCount() == 0) {
			db.execSQL("insert into New_Notice_Check values(" + pkcode
					+ ",0,0,0,0," + stucode + ")");
		}
		return true;
	}

	public boolean insert_New_Diary(int pkcode, int stucode) {
		
		cursor = db.rawQuery("select * from New_Notice_Check where Diary_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode, null);
		if (cursor.getCount() == 0) {
			db.execSQL("insert into New_Notice_Check values(0," + pkcode
					+ ",0,0,0," + stucode + ")");
		}
		return true;
	}

	public boolean insert_New_Photo(int pkcode, int stucode) {
	
		cursor = db.rawQuery("select * from New_Notice_Check where Photo_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode, null);
		if (cursor.getCount() == 0) {
			db.execSQL("insert into New_Notice_Check values(0,0," + pkcode
					+ ",0,0," + stucode + ")");
		}
		return true;
	}

	public boolean insert_New_Mas(int pkcode, int stucode) {
		stucode=0;
		cursor = db.rawQuery("select * from New_Notice_Check where Mas_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode, null);
		if (cursor.getCount() == 0) {
			db.execSQL("insert into New_Notice_Check values(0,0,0," + pkcode
					+ ",0," + stucode + ")");
		}
		return true;
	}

	public boolean insert_New_Cal(int pkcode, int stucode) {
		stucode=0;
		cursor = db.rawQuery("select * from New_Notice_Check where Cal_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode, null);
		if (cursor.getCount() == 0) {
			db.execSQL("insert into New_Notice_Check values(0,0,0,0," + pkcode
					+ "," + stucode + ")");
		}
		return true;
	}

	public boolean delete_Notice(int pkcode, int stucode) {
		db.execSQL("delete from Notice_Check where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode);

		return true;
	}

	public boolean delete_NoticeStamp(int pkcode, int stucode) {
		db.execSQL("delete from Notice_Stamp where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode);

		return true;
	}

	public boolean delete_Diary(int pkcode, int stucode) {
		db.execSQL("delete from Diary_Check where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode);

		return true;
	}

	public boolean delete_Photo(int pkcode, int stucode) {
		db.execSQL("delete from Photo_Check where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode);

		return true;
	}

	public boolean delete_PhotoStamp(int pkcode, int stucode) {
		db.execSQL("delete from Photo_Stamp where PK_Code="
				+ pkcode + " AND " + "Stu_Code=" + stucode);

		return true;
	}

	public boolean delete_New_Notice(int pkcode) {
	
		db.execSQL("delete from New_Notice_Check where Notice_Code="
				+ pkcode  );

		return true;
	}

	public boolean delete_New_Diary(int pkcode) {

		db.execSQL("delete from New_Notice_Check where Diary_Code="
				+ pkcode );

		return true;
	}

	public boolean delete_New_Photo(int pkcode) {
		
		db.execSQL("delete from New_Notice_Check where Photo_Code="
				+ pkcode  );

		return true;
	}

	public boolean delete_New_Mas(int pkcode) {
		db.execSQL("delete from New_Notice_Check where Mas_Code="
				+ pkcode  );
		return true;
	}

	public boolean delete_New_Cal(int pkcode) {

		db.execSQL("delete from New_Notice_Check where Cal_Code="
				+ pkcode  );
		return true;
	}
	
	
	public Cursor check_Err_Log( ){
		cursor = db.rawQuery("select * from Error_Log_Check",
				null);	
			return cursor;
		
	}
	
	
	
	public boolean delete_Err_Log() {
		db.execSQL("delete from Error_Log_Check ");
		return true;
	}
	
	public boolean insert_Err_Log(String sub,int api, String where, String email){
		
		db.execSQL("insert into Error_Log_Check values( " 
				+"datetime('now', 'localtime'),"
				+"'"+ sub +"',"
				+"'"+ api + "',"
				+"'"+ where + "',"
				+"'"+ email +"')");
		
		return true;		
	}
}
